//
//  TSSecuritySettingViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/6.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSSecuritySettingViewController: TSBaseViewController {

    var originPwdField : UITextField!
    var latestPwdField : UITextField!
    var confirmPwdField : UITextField!
    
    var cashOriginPwdField : UITextField!
    var cashLatestPwdField : UITextField!
    var cashConfirmPwdField : UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationTitle(title: "安全设置")
        
        let manager = DVSliderManager.init()
        manager.creatSlideView(vc:self,title:["登录密码","提款密码"])

        let array = manager.sliderView.subviews
        for index in 0..<array.count {
            let view = array[index]
            setupView(view, index: index)
        }
    }
    
    @objc private func loginReverseAction() {
        
        guard originPwdField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入旧登录密码")
            return
        }
        
        guard latestPwdField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入新登录密码")
            return
        }
        
        guard confirmPwdField.text?.count != 0 else {
            CBToast.showToastAction(message: "请确认新登录密码")
            return
        }
        
        guard latestPwdField.text == confirmPwdField.text else {
            CBToast.showToastAction(message: "新密码与确认密码输入不一致")
            return;
        }
        
        IQKeyboardManager.shared().resignFirstResponder()
        
        CBToast.showToastAction()
        TSNetworkManager.request(TMSNetworkTools.reverseLoginPassword(oldpwd: originPwdField.text!, password: latestPwdField.text!, repassword: confirmPwdField.text!), showHUD: true, success: { (data) in
            
            CBToast.hiddenToastAction()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
                CBToast.showToastAction(message: "修改成功")
            }
            
        }) { (error) in
            CBToast.hiddenToastAction()
        }
    }
    
    @objc private func loginResetAction() {
        
        originPwdField.text = ""
        latestPwdField.text = ""
        confirmPwdField.text = ""
    }
    
    @objc private func cashReverseAction() {
        
        guard cashOriginPwdField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入旧提款密码")
            return
        }
        
        guard cashLatestPwdField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入新提款密码")
            return
        }
        
        guard cashConfirmPwdField.text?.count != 0 else {
            CBToast.showToastAction(message: "请确认新提款密码")
            return
        }
        
        guard cashLatestPwdField.text == cashConfirmPwdField.text else {
            CBToast.showToastAction(message: "新密码与确认密码输入不一致")
            return;
        }
        
        IQKeyboardManager.shared().resignFirstResponder()
        
        CBToast.showToastAction()
        TSNetworkManager.request(TMSNetworkTools.reverseCashPassword(oldpwd: cashOriginPwdField.text!, password: cashLatestPwdField.text!, repassword: cashConfirmPwdField.text!), showHUD: true, success: { (data) in
            
            CBToast.hiddenToastAction()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
                CBToast.showToastAction(message: "修改成功")
            }
            
        }) { (error) in
            CBToast.hiddenToastAction()
        }
    }
    
    @objc private func cashResetAction() {
       
        cashOriginPwdField.text = ""
        cashLatestPwdField.text = ""
        cashConfirmPwdField.text = ""
    }

}

extension TSSecuritySettingViewController: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let iderntify:String = "swiftCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: iderntify)
        if(cell == nil){
            cell=UITableViewCell(style: UITableViewCell.CellStyle.default
                , reuseIdentifier: iderntify);}
        cell?.textLabel?.text = "\(indexPath.section)"
        cell?.backgroundColor = UIColor.gray
        return cell!
    }
    
}

extension TSSecuritySettingViewController {
    
    private func setupView(_ superView:UIView, index:Int) {
        if index == 0 {
            setupLoginPassword(superView)
        } else {
            setupCashPassword(superView)
        }
    }
    
    private func setupLoginPassword(_ superView:UIView) {
        
        originPwdField = createInputField(placeholder: "请输入旧登录密码")
        superView.addSubview(originPwdField)
        originPwdField.snp.makeConstraints { (make) in
            make.left.equalTo(90)
            make.top.equalTo(42)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 90 - 15, height: 50))
        }
        
        let oldPwdLabel = createLabel(text: "旧密码")
        superView.addSubview(oldPwdLabel)
        oldPwdLabel.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.centerY.equalTo(originPwdField)
            make.width.equalTo(60)
        }
        
        let line1 = drawLine()
        superView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalTo(originPwdField.snp.bottom)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*12, height: 1))
        }
                
        latestPwdField = createInputField(placeholder: "请输入新登录密码")
        superView.addSubview(latestPwdField)
        latestPwdField.snp.makeConstraints { (make) in
            make.left.equalTo(originPwdField)
            make.top.equalTo(line1.snp.bottom)
            make.size.equalTo(originPwdField)
        }
        
        let newPwdLabel = createLabel(text: "新密码")
        superView.addSubview(newPwdLabel)
        newPwdLabel.snp.makeConstraints { (make) in
            make.left.width.equalTo(oldPwdLabel)
            make.centerY.equalTo(latestPwdField)
        }
        
        let line2 = drawLine()
        superView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(latestPwdField.snp.bottom)
            make.size.equalTo(line1)
        }
                        
        confirmPwdField = createInputField(placeholder: "请确认新登录密码")
        superView.addSubview(confirmPwdField)
        confirmPwdField.snp.makeConstraints { (make) in
            make.left.equalTo(originPwdField)
            make.top.equalTo(line2.snp.bottom)
            make.size.equalTo(originPwdField)
        }
        
        let confirmPwdLabel = createLabel(text: "确认密码")
        superView.addSubview(confirmPwdLabel)
        confirmPwdLabel.snp.makeConstraints { (make) in
            make.left.width.equalTo(oldPwdLabel)
            make.centerY.equalTo(confirmPwdField)
        }
        
        let line3 = drawLine()
        superView.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(confirmPwdField.snp.bottom)
            make.size.equalTo(line1)
        }
        
        let infoLabel = createLabel(text: "备注：请妥善保管好您的登录密码，如遗忘请联系在线客服处理")
        infoLabel.textColor = BASE_RED_COLOR
        infoLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        superView.addSubview(infoLabel)
        infoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(line3.snp.bottom).offset(15)
        }
        
        let reverseBtn = UIButton.init(type: .system)
        reverseBtn.setTitle("修改", for: .normal)
        reverseBtn.setTitleColor(WHITE_COLOR, for: .normal)
        reverseBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        reverseBtn.backgroundColor = BASE_RED_COLOR
        reverseBtn.layer.cornerRadius = 24
        reverseBtn.layer.masksToBounds = true
        reverseBtn.addTarget(self, action: #selector(loginReverseAction), for: .touchUpInside)
        superView.addSubview(reverseBtn)
        reverseBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(infoLabel.snp.bottom).offset(30)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
        
//        let resetBtn = UIButton.init(type: .system)
//        resetBtn.setTitle("重置", for: .normal)
//        resetBtn.setTitleColor(UIColor.hexadecimalColor(hexadecimal: "#282828"), for: .normal)
//        resetBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
//        resetBtn.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F7F7F7")
//        resetBtn.layer.cornerRadius = 24
//        resetBtn.layer.masksToBounds = true
//        resetBtn.addTarget(self, action: #selector(loginResetAction), for: .touchUpInside)
//        superView.addSubview(resetBtn)
//        resetBtn.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(reverseBtn.snp.bottom).offset(15)
//            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
//        }
//
//        let alertLabel = createLabel(text: "未满18周岁禁止购买")
//        alertLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#686868")
//        alertLabel.font = UIFont.systemFont(ofSize: 10, weight: .medium)
//        superView.addSubview(alertLabel)
//        alertLabel.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(resetBtn.snp.bottom).offset(15)
//        }
    }
    
    private func setupCashPassword(_ superView:UIView) {
        
        cashOriginPwdField = createInputField(placeholder: "请输入旧提款密码")
        superView.addSubview(cashOriginPwdField)
        cashOriginPwdField.snp.makeConstraints { (make) in
            make.left.equalTo(90)
            make.top.equalTo(42)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 90 - 15, height: 50))
        }
        
        let oldPwdLabel = createLabel(text: "旧密码")
        superView.addSubview(oldPwdLabel)
        oldPwdLabel.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.centerY.equalTo(cashOriginPwdField)
            make.width.equalTo(60)
        }
        
        
        let line1 = drawLine()
        superView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(cashOriginPwdField.snp.bottom)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*15, height: 1))
        }
                
        cashLatestPwdField = createInputField(placeholder: "请输入新提款密码")
        superView.addSubview(cashLatestPwdField)
        cashLatestPwdField.snp.makeConstraints { (make) in
            make.left.equalTo(cashOriginPwdField)
            make.top.equalTo(line1.snp.bottom)
            make.size.equalTo(cashOriginPwdField)
        }
        
        let newPwdLabel = createLabel(text: "新密码")
        superView.addSubview(newPwdLabel)
        newPwdLabel.snp.makeConstraints { (make) in
            make.left.width.equalTo(oldPwdLabel)
            make.centerY.equalTo(cashLatestPwdField)
        }
        
        let line2 = drawLine()
        superView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(cashLatestPwdField.snp.bottom)
            make.size.equalTo(line1)
        }
                        
        cashConfirmPwdField = createInputField(placeholder: "请确认新提款密码")
        superView.addSubview(cashConfirmPwdField)
        cashConfirmPwdField.snp.makeConstraints { (make) in
            make.left.equalTo(cashOriginPwdField)
            make.top.equalTo(line2.snp.bottom)
            make.size.equalTo(cashOriginPwdField)
        }
        
        let confirmPwdLabel = createLabel(text: "确认密码")
        superView.addSubview(confirmPwdLabel)
        confirmPwdLabel.snp.makeConstraints { (make) in
            make.left.width.equalTo(oldPwdLabel)
            make.centerY.equalTo(cashConfirmPwdField)
        }
        
        let line3 = drawLine()
        superView.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(cashConfirmPwdField.snp.bottom)
            make.size.equalTo(line1)
        }
        
        let infoLabel = createLabel(text: "备注：请妥善保管好您的提款密码，如遗忘请联系在线客服处理")
        infoLabel.textColor = BASE_RED_COLOR
        infoLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        superView.addSubview(infoLabel)
        infoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(line3.snp.bottom).offset(15)
        }
        
        let reverseBtn = UIButton.init(type: .system)
        reverseBtn.setTitle("修改", for: .normal)
        reverseBtn.setTitleColor(WHITE_COLOR, for: .normal)
        reverseBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        reverseBtn.backgroundColor = BASE_RED_COLOR
        reverseBtn.layer.cornerRadius = 24
        reverseBtn.layer.masksToBounds = true
        reverseBtn.addTarget(self, action: #selector(cashReverseAction), for: .touchUpInside)
        superView.addSubview(reverseBtn)
        reverseBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(infoLabel.snp.bottom).offset(30)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
        
//        let resetBtn = UIButton.init(type: .system)
//        resetBtn.setTitle("重置", for: .normal)
//        resetBtn.setTitleColor(UIColor.hexadecimalColor(hexadecimal: "#282828"), for: .normal)
//        resetBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
//        resetBtn.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F7F7F7")
//        resetBtn.layer.cornerRadius = 24
//        resetBtn.layer.masksToBounds = true
//        resetBtn.addTarget(self, action: #selector(cashResetAction), for: .touchUpInside)
//        superView.addSubview(resetBtn)
//        resetBtn.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(reverseBtn.snp.bottom).offset(15)
//            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
//        }
//
//        let alertLabel = createLabel(text: "未满18周岁禁止购买")
//        alertLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#686868")
//        alertLabel.font = UIFont.systemFont(ofSize: 10, weight: .medium)
//        superView.addSubview(alertLabel)
//        alertLabel.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(resetBtn.snp.bottom).offset(15)
//        }
    }
    
    func createInputField(placeholder:String) -> UITextField {
        
        let string = placeholder
        let attributeStr = NSMutableAttributedString.init(string: string)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: "#989898"), NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: .medium)], range: NSMakeRange(0, string.count))
        
        let textField = UITextField.init()
        textField.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        textField.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        textField.attributedPlaceholder = (attributeStr.copy() as! NSAttributedString)
        textField.isSecureTextEntry = true
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        return textField
    }

    func createLabel(text:String) -> UILabel {
        
        let label = UILabel.init()
        label.text = text
        label.textColor = UIColor.hexadecimalColor(hexadecimal: "#3B3B3B")
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return label
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        return line
    }
}

